package com.jettech.oa.jbpm.handler;

import java.util.List;

import org.jbpm.graph.exe.ExecutionContext;
import org.jbpm.taskmgmt.def.AssignmentHandler;
import org.jbpm.taskmgmt.exe.Assignable;
import org.springframework.beans.factory.annotation.Autowired;

import com.jettech.oa.service.UserService;
/**
 * 将任务提交给指定的角色人员
 * @author haichenwang
 *
 */
public class RoleAssignmentHandler extends BaseAutowire implements
		AssignmentHandler {
	private static final long serialVersionUID = 5544825155037737218L;
	private String roleName;
	@Autowired
	private UserService userService;
	public void assign(Assignable assignable, ExecutionContext executionContext) throws Exception {
		if(roleName == null){
			throw new RuntimeException("您必须设置角色名称");
		}
		List<String> userLoginNames = userService.findUserLoginNameOfRole(roleName);
		if(userLoginNames == null || (userLoginNames!=null && userLoginNames.size()<=0)){
			throw new RuntimeException("角色["+roleName+"]下面没有任何用户,无法完成任务分配.");
		}
		String[] pooledActors = (String[])userLoginNames.toArray(new String[userLoginNames.size()]);
		assignable.setPooledActors(pooledActors);
	}

}
